{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# RASA Chatbot 在 Kubernetes 中的部署实战\n",
    "\n",
    "在这个文章中，我们将 RASA Chatbot Server 部署到一个 Kubernetes 集群中。我们将使用 AWS 作为基础平台，\n",
    "在 AWS China Region，您可以选择使用 [nwcdlabs/kops-cn](https://github.com/nwcdlabs/kops-cn) 自建 K8S, \n",
    "在 AWS Global Region, 您可以直接选择使用托管的 K8S平台 [EKS](https://aws.amazon.com/eks/).\n",
    "\n",
    "## 在容器中运行 RASA\n",
    "需要了解如何在 K8S 中运行 RASA, 我们要首先了解如何在 docker 下运行 RASA 项目。RASA 支持将放置在\n",
    "\n",
    "```shell script\n",
    "docker run --env AWS_ACCESS_KEY_ID=<aws-access-key-id> \\\n",
    "--env AWS_SECRET_ACCESS_KEY=<aws-secret-access-key> \\\n",
    "--env AWS_DEFAULT_REGION=<aws-region> --env BUCKET_NAME=<bucket-name> \\\n",
    "-p 5005:5005 \\\n",
    "<docker-image>:latest \\\n",
    "run -m <model-name> \\\n",
    "--enable-api \\\n",
    "--remote-storage aws\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## K8S 部署\n",
    "\n",
    "1. 先将 RASA 的基础环境制作成基础镜像，基础镜像只包含 Corpus, 并不包含 model, 请参考 [Dockerfile](Dockerfile)\n",
    "1. 上传镜像到 Amazon ECR\n",
    "1. 上传训练好的模型文件到某一个 Amazon S3 Bucket\n",
    "1. 创建一个 AK/SK, 建议选择只能读取某个 S3 某个 prefix 的权限\n",
    "1. [k8s_deployment/](k8s_deployment/) 目录下提供了 RASA service 的部署 YAML 文件\n",
    "\n",
    "```\n",
    "- image: 057005827724.dkr.ecr.cn-northwest-1.amazonaws.com.cn/rasa-jw\n",
    "env:\n",
    "  - name: AWS_ACCESS_KEY_ID\n",
    "    value: <aws-access-key-id>\n",
    "  - name: AWS_SECRET_ACCESS_KEY\n",
    "    value: <aws-secret-access-key>\n",
    "  - name: AWS_DEFAULT_REGION\n",
    "    value: cn-northwest-1\n",
    "  - name: BUCKET_NAME\n",
    "    value: rasa-dev\n",
    "args: [\"run\", \"-m\", \"20190823-222532.tar.gz\", \"--enable-api\", \"--remote-storage\", \"aws\"]\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
